/*
 * Copyright 2004-2006 公司名
 * 本软件及其所有拷贝的名称，与光盘上或本软件中注明的公司同在。
 * 本软件及文档享有版权，并受国家版权法及国际协约条款的保护。
 * 您不可以从本软件中去掉其版权声明；并保证为本软件的拷贝（本部或部分）复制版权声明。
 * 您同意制止以任何形式非法拷贝本软件及文档。
 * 版权信息参见：
 *      http://www.companyname.cn/licenses
 */

package com.companyname.common.viewframe.valid;

import java.util.LinkedHashSet;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

import com.companyname.common.sysframe.define.SysFrameParams;
import com.companyname.common.sysframe.manager.OrgManager;
import com.companyname.common.sysframe.manager.PostManager;
import com.companyname.common.sysframe.manager.RoleManager;
import com.companyname.common.util.mvc.valid.UserValidator;
import com.companyname.common.util.mvc.valid.ValidatedResult;
import com.companyname.common.viewframe.manager.HomeManager;
import com.companyname.common.viewframe.model.Home;
import com.companyname.common.viewframe.util.CompanyNameViewFrameException;

public class AddHomeValidator extends UserValidator
{
        static Logger logger = Logger.getLogger(AddHomeValidator.class);

        @Override
        public void validing(HttpServletRequest request, ValidatedResult vr, String[] params)
        {
                logger.debug("开始校验");
                HomeManager homeManager = (HomeManager)this.getBean("homeManager");
                String code = this.validString(request,vr,"code",1,50);
                if(vr.isValidated("code"))
                {
                        if(homeManager.getByCode(code) != null)
                        {
                                vr.setErrMess("code","已经被使用");
                        }
                }
                logger.debug("code : " + code);
                
                String name = this.validString(request,vr,"name",1,50);
                logger.debug("name : " + name);
                
                String url = this.validString(request,vr,"url",0,200);
                logger.debug("url : " + url);
                
                String[] tIds = request.getParameterValues("tIds");
                String[] tTypes = request.getParameterValues("tTypes");
                Set orgs = new LinkedHashSet();
                Set roles = new LinkedHashSet();
                Set posts = new LinkedHashSet();
                if(tIds != null && tTypes != null)
                {
                        OrgManager orgManager = (OrgManager)this.getBean("orgManager");
                        RoleManager roleManager = (RoleManager)this.getBean("roleManager");
                        PostManager postManager = (PostManager)this.getBean("postManager");
                        logger.debug("取得orgs - roles - posts");
                        for(int i = 0; i < tIds.length; i ++ )
                        {
                                int type = Integer.parseInt(tTypes[i]);
                                switch(type)
                                {
                                        case SysFrameParams.ORG : 
                                        {
                                                orgs.add(orgManager.get(tIds[i]));
                                                break;
                                        }
                                        case SysFrameParams.ROLE : 
                                        {
                                                roles.add(roleManager.get(tIds[i]));
                                                break;
                                        }
                                        case SysFrameParams.POST : 
                                        {
                                                posts.add(postManager.get(tIds[i]));
                                                break;
                                        }
                                        default:
                                        {
                                                throw new CompanyNameViewFrameException("未知目标类型:" + type);
                                        }
                                }
                        }
                }
                
                String remark = this.validString(request,vr,"remark",0,500);
                logger.debug("remark.length() : " + remark.length());
                
                if(vr.isAllValidated())
                {
                        Home home = new Home();
                        if(tIds != null && tTypes != null)
                        {
                                home.setOrgs(orgs);
                                home.setRoles(roles);
                                home.setPosts(posts);
                        }
                        home.setCode(code);
                        home.setName(name);
                        home.setUrl(url);
                        home.setRemark(remark);
                        request.setAttribute(Home.class.getName(),home);
                }
        }
}
